﻿//-----------------------------------------------------------------------
// <copyright>
//     Copyright (c) Artur Mustafin. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Excel.Expressions.Compiler;

namespace Excel.Expressions.Classes
{
    public class Cell
    {
#if DEBUG
        private string _inputReferencesText;
        private string _outputReferencesText;
#endif
        private int _id;
        private readonly int _row;
        private readonly int _column;
        private const int size = 256;

        private static Cell[,] _cells = new Cell[size, size];

        static Cell()
        {
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    _cells[i, j] = new Cell(i * size + j, i, j);
                }
            }
        }

        private bool _visited = false;

        private double _value;

        private CellExpression _expression;

        private readonly string _name;

        private Cell(int id, int row, int column)
        {
            _id = id;
            _row = row;
            _column = column;
            _name = string.Format("{0}{1}", Cell.GetRow(row), Cell.GetCol(column));
            _inputReferences = new HashSet<Cell>();
            _outputReferences = new HashSet<Cell>();
        }

        public static string GetRow(int rowIndex)
        {
            rowIndex += 1;
            List<char> chars = new List<char>();
            do
            {
                if (rowIndex % 26 == 0)
                {
                    chars.Add('Z');
                    rowIndex /= 26;
                    rowIndex -= 1;
                    continue;
                }
                char ch = (char)('@' + rowIndex % 26);
                chars.Add(ch);
                rowIndex /= 26;
            } while (rowIndex > 0);
            chars.Reverse();
            return new string(chars.ToArray());
        }

        internal static int GetRow(string name)
        {
            int rowIndex = 0;
            foreach (char ch in name)
            {
                rowIndex *= 26;
                rowIndex += (ch - 'A' + 1);
            }
            return rowIndex - 1;
        }

        public static string GetCol(int colIndex)
        {
            List<char> chars = new List<char>();
            do
            {
                char ch = (char)('0' + colIndex % 10);
                chars.Add(ch);
                colIndex /= 10;
            } while (colIndex > 0);
            chars.Reverse();
            return new string(chars.ToArray());
        }

        internal static int GetCol(string name)
        {
            int colIndex = 0;
            foreach (char ch in name)
            {
                colIndex = colIndex * 10 + ch - '0';
            }
            return colIndex;
        }

        public int Id
        {
            get
            {
                return _id;
            }
        }

        public static Cell GetCell(int row, int column)
        {
            return _cells[row, column];
        }

        public bool HasExpression
        {
            get
            {
                return _expression != null;
            }
        }

        public void Eval()
        {
            if (_visited == false)
            {
                _visited = true;
                if (_expression != null)
                {
                    _value = _expression.Eval();
                }
                else
                {
                    _value = 0;
                }
                if (_outputReferences.Count > 0)
                {
                    foreach (Cell cell in _outputReferences)
                    {
                        cell.Eval();
                    }
                }
            }
        }

        public void Visit(Action<Cell> action)
        {
            if (_visited == true)
            {
                _visited = false;
                if (_inputReferences.Count > 0)
                {
                    foreach (var cell in _inputReferences)
                    {
                        cell.Visit(action);
                    }
                }
                Eval();
                if (action != null)
                {
                    action(this);
                }
            }
        }

        public double Double
        {
            get
            {
                return _value;
            }
        }

        public CellExpression Expression
        {
            get
            {
                return _expression;
            }
            set
            {
                if (_expression != value)
                {
                    _expression = value;
                    _visited = false;
                    Eval();
                }
            }
        }

        public int Column
        {
            get
            {
                return _column;
            }
        }

        public int Row
        {
            get
            {
                return _row;
            }
        }

        public string Value
        {
            get
            {
                if (_expression != null)
                {
                    return _value.ToString(CultureInfo.InvariantCulture.NumberFormat);
                }
                return string.Empty;
            }
        }

        private string _text;

        public string Text
        {
            get
            {
                return _text;
            }
            set
            {
                _text = value;
            }
        }

        private readonly HashSet<Cell> _outputReferences;

        private readonly HashSet<Cell> _inputReferences;

        private void Subscribe(Cell cell)
        {
            cell.SubscribeInput(this);
            SubscribeOutput(cell);
        }

        private void Unsubscribe(Cell cell)
        {
            cell.UnsubscribeInput(this);
            UnsubscribeOutput(cell);
        }

        private void SubscribeInput(Cell cell)
        {
#if DEBUG
            _inputReferencesText = null;
#endif
            _inputReferences.Add(cell);
        }

        private void UnsubscribeInput(Cell cell)
        {
#if DEBUG
            _inputReferencesText = null;
#endif
            _inputReferences.Remove(cell);
        }

        private void SubscribeOutput(Cell cell)
        {
#if DEBUG
            _outputReferencesText = null;
#endif
            _outputReferences.Add(cell);
        }

        private void UnsubscribeOutput(Cell cell)
        {
#if DEBUG
            _outputReferencesText = null;
#endif
            _outputReferences.Remove(cell);
        }

        public void Clear()
        {
#if DEBUG
            _inputReferencesText = null;
            _outputReferencesText = null;
#endif
            _inputReferences.Clear();
            _outputReferences.Clear();
            _expression = null;
            _text = null;
            _value = 0;
        }

        public void Subscribe()
        {
            HashSet<Cell> outputReferences = new HashSet<Cell>();
            Decompile(outputReferences, Expression);
            foreach (Cell cell in outputReferences)
            {
                cell.SubscribeInput(this);
                this.SubscribeOutput(cell);
            }
        }

        public void Unsubscribe()
        {
            HashSet<Cell> outputReferences = new HashSet<Cell>();
            Decompile(outputReferences, Expression);
            foreach (Cell cell in outputReferences)
            {
                cell.UnsubscribeInput(this);
                this.UnsubscribeOutput(cell);
            }
        }

#if DEBUG
        public override string ToString()
        {
            if (_inputReferencesText == null)
            {
                _inputReferencesText = GetReferences(_inputReferences, _inputReferences.Count);
            }
            if (_outputReferencesText == null)
            {
                _outputReferencesText = GetReferences(_outputReferences, _outputReferences.Count);
            }
            bool inputIsNull = string.IsNullOrEmpty(_inputReferencesText);
            bool outputIsNull = string.IsNullOrEmpty(_outputReferencesText);
            if (!inputIsNull && !outputIsNull)
            {
                return string.Format("{0}->{1}->{2}", _outputReferencesText, _name, _inputReferencesText);
            }
            if (!inputIsNull)
            {
                return string.Format("{0}->{1}", _name, _inputReferencesText);
            }
            if (!outputIsNull)
            {
                return string.Format("{0}->{1}", _outputReferencesText, _name);
            }
            return _name;
        }
#else
        public override string ToString()
        {
            return _name;
        }
#endif

        private string GetReferences(IEnumerable<Cell> cells, int count)
        {
            IEnumerator<Cell> enumerator = cells.GetEnumerator();
            StringBuilder sb = new StringBuilder();
            if (enumerator.MoveNext())
            {
                Cell cell = enumerator.Current;
                sb.Append(cell.Name);
                int index = 0;
                while (enumerator.MoveNext())
                {
                    if (index == 0 && index < count - 2)
                    {
                        sb.Append(",...");
                        index++;
                        continue;
                    }
                    if (index > 0 && index < count - 2)
                    {
                        index++;
                        continue;
                    }
                    cell = enumerator.Current;
                    sb.Append(',');
                    sb.Append(cell.Name);
                    index++;
                }
            }
            return sb.ToString();
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public bool Validate(CellExpression expression)
        {
            foreach (Cell cell in Decompile(expression))
            {
                if (cell == this)
                {
                    return false;
                }
            }
            return true;
        }

        private HashSet<Cell> Decompile(CellExpression expression)
        {
            HashSet<Cell> cells = new HashSet<Cell>();
            Decompile(cells, expression);
            return cells;
        }

        public void Decompile(HashSet<Cell> cells, CellExpression expression)
        {
            if (expression != null)
            {
                Stack<CellExpression> expressions = new Stack<CellExpression>();
                expressions.Push(expression);
                do
                {
                    CellExpression current = expressions.Pop();
                    if (current.Children != null)
                    {
                        foreach (CellExpression node in current.Children)
                        {
                            CellReference reference = node as CellReference;
                            if (reference != null)
                            {
                                if (cells.Add(reference.Cell))
                                {
                                    if (reference.Cell.Expression != null)
                                    {
                                        expressions.Push(reference.Cell.Expression);
                                    }
                                }
                            }
                            else
                            {
                                if (node != null)
                                {
                                    expressions.Push(node);
                                }
                            }
                        }
                    }
                    CellReference referenced = current as CellReference;
                    if (referenced != null)
                    {
                        if (cells.Add(referenced.Cell))
                        {
                            if (referenced.Cell.Expression != null)
                            {
                                expressions.Push(referenced.Cell.Expression);
                            }
                        }
                    }
                }
                while (expressions.Count > 0);
            }
        }

        private string _updateText;
        private CellExpression _updateExpression;

        public void BeginUpdate()
        {
            _updateText = _text;
            _updateExpression = _expression;
        }

        public void EndUpdate()
        {
            _updateText = null;
            _updateExpression = null;
        }

        public void CancelUpdate()
        {
            _text = _updateText;
            _expression = _updateExpression;
        }

        internal static Range GetRange(int id1, int id2)
        {
            if (id1 > id2)
            {
                int temp = id1;
                id1 = id2;
                id2 = temp;
            }
            return new Range(id1 / size, id1 % size, id2 / size, id2 % size);
        }
    }
}
